package com.rymcu.subject.service.impl;

import com.rymcu.subject.entity.Question;
import com.rymcu.subject.entity.SubjectFile;
import com.rymcu.subject.entity.SubjectOption;
import com.rymcu.subject.entity.SubjectQuestion;
import com.rymcu.subject.mapper.SubjectFileMapper;
import com.rymcu.subject.mapper.SubjectOptionMapper;
import com.rymcu.subject.mapper.SubjectQuestionMapper;
import com.rymcu.subject.service.SubjectFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 🐛
 * @date Created in 2021/8/5 16:48
 */
@Service
public class SubjectFileServiceImpl implements SubjectFileService {

    @Autowired
    private SubjectFileMapper subjectFileMapper;

    @Autowired
    private SubjectOptionMapper subjectOptionMapper;

    @Autowired
    private SubjectQuestionMapper subjectQuestionMapper;

    @Override
    public long insert(SubjectFile subjectFile) {
        return subjectFileMapper.insert(subjectFile);
    }


    @Override
    public long insertSubjects(
            List<Question> questionList,
            long sfId,
            long createdBy,
            Date now
    ) {
        if (questionList == null || questionList.size() == 0) {
            return 0;
        }

        questionList.stream().forEach(question -> {
            final SubjectQuestion subjectQuestion = new SubjectQuestion();
            subjectQuestion.setAnswer(question.getAnswer());
            subjectQuestion.setQuestionContent(question.getQuestionContent());
            subjectQuestion.setSfId(sfId);
            subjectQuestion.setCreatedTime(now);
            subjectQuestion.setUserId(createdBy);
            subjectQuestion.setQuestionType(1);
            subjectQuestionMapper.insert(subjectQuestion);
            final long sqId = subjectQuestion.getId();
            question.getOptions().forEach(subjectOption -> {
                final SubjectOption option = new SubjectOption();
                option.setOptionContent(subjectOption.getOptionContent());
                option.setOptionName(subjectOption.getOptionName());
                option.setSubjectQuestionId(sqId);
                subjectOptionMapper.insert(option);
            });

        });

        return 1;
    }
}
